package com.zhku.onlineshop.Servlet;


import com.zhku.onlineshop.Pojo.Order;
import com.zhku.onlineshop.Pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/orders")
@Slf4j
public class OrderServlet {
    @Autowired
    private com.zhku.onlineshop.Service.OrderService orderService;

    @PostMapping
    public Result submitOrder(@RequestBody Order order) {
        Integer order_id = orderService.submitOrder(order);
        log.info(String.valueOf(order));
        if (order_id!=null)
            return Result.success(order_id);
        else
            return Result.error("订单添加失败");
    }
    @PutMapping("/{id}/status")
    public Result changeOrderStatus(@PathVariable Integer id, @RequestParam String status) {
        if(orderService.changeOrderStatus(id, status))
            return Result.success();
        else
            return Result.error("修改订单状态失败");
    }

    @DeleteMapping("/{id}")
    public Result deleteOrder(@PathVariable Integer id) {
        if(orderService.removeOrder(id))
            return Result.success();
        else
            return Result.error("删除订单失败");
    }

    @GetMapping
    public Result getOrdersByCustomerId(@RequestParam Integer customer_id) {
        List<Order> orderlist = orderService.getOrdersByCustomerId(customer_id);
        if (orderlist !=null)
            return Result.success(orderlist);
        else
            return Result.error("订单为空");
    }

    //根据订单状态获取List<Order>
    @GetMapping("/getOrderBystatus")
    public Result getOrderBystatus(Integer status){
        List<Order> orderlist = orderService.getOrderBystatus(status);
        if (orderlist !=null)
            return Result.success(orderlist);
        else
            return Result.error("订单为空");
    }

}
